<?php
declare (strict_types = 1);

namespace app\mobile\controller;

use think\facade\Request;
use think\facade\Db;
use think\facade\Cache;
use \app\common\Time;

class Team extends \app\mobile\controller\Base
{
  public function search()
  {
    
    $searchKey = input('searchKey');
    $date = input('date');
    $level = input('level');
    $pageIndex = input('pageIndex',1);

    $query = [];
    if ($searchKey) {
      $query[] = ['id|nick','like',"%{$searchKey}%"];
      $date ='';
      $level = 3;
    }

    if ($date == 'today') {
      list($st,$et) = Time::today();
      $query[] = ['ctime','>',$st];
      $query[] = ['ctime','<',$et];
    }elseif ($date == 'month') {
      list($st,$et) = Time::month();
      $query[] = ['ctime','>',$st];
      $query[] = ['ctime','<',$et];
    }elseif ($date == 'last month') {
      list($st,$et) = Time::lastMonth();
      $query[] = ['ctime','>',$st];
      $query[] = ['ctime','<',$et];
    }

    if ($level == 1) {
      $query[] = ['pid','=',$this->user['id']];
    }elseif ($level == 2) {
      $uids = getTeamUidsNoMy($this->uid);
      $query[] = ['pid','in',$uids];
    }elseif ($level == 3) {
      $uids = getTeamUidsNoMy($this->uid);
      $query[] = ['pid','in',$uids];
    }
    $rows = Db::name('user')
              ->field('id,nick,head,ctime')
              ->where($query)
              ->page($pageIndex,10)
              ->select()->toArray();
    foreach ($rows as &$k) {
      //个人订单
      $user_order_count = Db::name('order')->where('uid',$k['id'])->count('id');
      $k['user_order_count'] = $user_order_count;

      //团队订单
      $uids = getTeamUidsNoMy($k['id']);
      $k['team_order_count'] = 0;
      $k['yesterday_profit'] = 0;
      $k['yesterday_profit'] = 0;


      if ($uids) {
        $team_order_count = Db::name('order')
                ->whereIn('uid',$uids)
                ->count('id');
        $k['team_order_count'] = $team_order_count;

        //昨日收益
        $yesterday_profit = Db::name('reward')
                  ->whereIn('uid',$uids)
                  ->sum('fee');
        $k['yesterday_profit'] = $yesterday_profit;
        // 上月收益
        $last_month_profit = Db::name('reward')
                ->whereIn('uid',$uids)
                ->sum('fee');
        $k['last_month_profit'] = $last_month_profit;
      }
      
      # code...
    }
    $r['rows'] = $rows;
    return $this->doSuccess('ok',$r);
    # code...
  }
  // 我的上级用户
  public function getPuser()
  {
    $user = Db::name('user')
            ->where('id',$this->user['pid'])
            // ->where('id','>',0)
            ->find();
    return $this->doSuccess('ok',$user);
  }
  //好友统计
  public function getFriendsCount()
  {
    $date = input('date');
    $level = input('level');

    $query = [];
    if ($level == 1) {
      $query[] = ['pid','=',$this->user['id']];
    }elseif ($level == 2) {
      $uids = getTeamUidsNoMy($this->uid);
      $query[] = ['pid','in',$uids];
    }elseif ($level == 3) {
      $uids = getTeamUidsNoMy($this->uid);
      $query[] = ['pid','in',$uids];
    }
    //所有
    $r = array();
    $r['all'] = Db::name('user')
                ->where($query)
                ->count('id');

    //今日
    $r['today'] = Db::name('user')
                ->where($query)
                ->whereTime('ctime','today')
                ->count('id');
    
    //本月
    $r['month'] = Db::name('user')
                ->where($query)
                ->whereTime('ctime','month')
                ->count('id');

    //上月
    $r['lastmonth'] = Db::name('user')
                ->where($query)
                ->whereTime('ctime','last month')
                ->count('id');
    return $this->doSuccess('ok',$r);

  }


}